from rest_framework import serializers
from .models import ProductCategory, Product, ProductReview, Banner, HistoryModel


# 修改 ProductCategorySerializer
class ProductCategorySerializer(serializers.ModelSerializer):
    subcategories = serializers.SerializerMethodField()

    class Meta:
        model = ProductCategory
        fields = '__all__'

    def get_subcategories(self, obj):
        # 如果是父分类，返回所有子分类
        if not obj.parent:
            subcategories = ProductCategory.objects.filter(parent=obj)
            return ProductCategorySerializer(subcategories, many=True).data
        return None


class ProductSerializer(serializers.ModelSerializer):
    """保险金融产品序列化器"""
    category_name = serializers.CharField(source='category.name', read_only=True)
    category_id = serializers.IntegerField(source='category.id', read_only=True)
    # 添加保险金融特有字段的序列化
    coverage_amount = serializers.DecimalField(max_digits=15, decimal_places=2, read_only=True)
    coverage_period = serializers.CharField(read_only=True)
    target_audience = serializers.CharField(read_only=True)
    annual_return = serializers.DecimalField(max_digits=5, decimal_places=2, read_only=True)
    risk_level = serializers.CharField(read_only=True)

    # 添加图片字段
    img_url = serializers.CharField(read_only=True)

    class Meta:
        model = Product
        fields = [
            'id', 'name', 'code', 'category', 'category_name', 'category_id',
            'description', 'price', 'premium', 'coverage_amount', 'coverage_period',
            'target_audience', 'features', 'claim_process', 'annual_return', 'risk_level',
            'is_active', 'created_at', 'updated_at', 'img_url'  # 添加img_url
        ]


class ProductDetailSerializer(serializers.ModelSerializer):
    """保险金融产品详情序列化器"""
    category = ProductCategorySerializer(read_only=True)

    # 添加图片字段
    img_url = serializers.CharField(read_only=True)

    class Meta:
        model = Product
        fields = [
            'id', 'name', 'code', 'category', 'description',
            'price', 'premium', 'coverage_amount', 'coverage_period',
            'target_audience', 'features', 'claim_process', 'annual_return', 'risk_level',
            'is_active', 'created_at', 'updated_at', 'img_url'  # 添加img_url
        ]


class ProductReviewSerializer(serializers.ModelSerializer):
    """产品评价序列化器"""
    username = serializers.CharField(source='user.username', read_only=True)
    user_avatar = serializers.SerializerMethodField()

    class Meta:
        model = ProductReview
        fields = [
            'id', 'username', 'user_avatar', 'rating', 'service_rating',
            'content', 'claim_experience', 'created_at'
        ]

    def get_user_avatar(self, obj):
        # 简单的用户头像生成（实际项目中应从用户模型获取）
        return f"https://ui-avatars.com/api/?name={obj.user.username}&background=random"


# goods/serializers.py




class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields ='__all__'


class HistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = HistoryModel
        fields ='__all__'
